﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using System.Reflection;
using System.Data.Linq.Mapping;
using BulletOnRails4NET.Models;

namespace BulletOnRails4NET.Controllers
{
    public class CrudViewModel
    {
        public CrudModel DBObject { get; set; }
        public PropertyInfo[] EditableProperties { get; set; }
        public Dictionary<string, string> CustomFields { get; set; }

        public CrudViewModel(CrudModel crud_model, PropertyInfo[] editable_properties)
        {
            DBObject = crud_model;
            EditableProperties = editable_properties;
            CustomFields = null;
        }

        public CrudViewModel(CrudModel crud_model, PropertyInfo[] editable_properties, List<AssociationQueryInfo> associations_info)
        {
            DBObject = crud_model;
            EditableProperties = editable_properties;
            //Fill in the current value of the FK into the associations_info
            foreach (AssociationQueryInfo association_info in associations_info)
            {
                object curr_id = crud_model.GetType().GetProperty(association_info.FkColumnName).GetValue(crud_model, null);
                if (curr_id == null)
                    association_info.CurrIdSelected = -1;
                else
                    association_info.CurrIdSelected = (int)curr_id;
            }
            CustomFields = AssociationFormHelper.CreateAssociationHTMLFields(associations_info);
        }

    }
}
